<template>
  <div class="">
    <h1>学员管理</h1>
    <el-form :inline="true" :model="ser" class="demo-form-inline">
      <el-form-item label="用户名">
        <el-input v-model="ser.name" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="ser.mobile" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-button type="primary" @click="removeall">选中删除</el-button>

    <el-table
      ref="box"
      :data="reslist"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="change"
    >
      <el-table-column type="selection" width="55"> </el-table-column>

      <el-table-column prop="nickname" label="姓名" width="120">
        <template slot-scope="scope">
          <!-- <img :src="scope.row.avatar" alt="" style="width:" /> -->
          <el-avatar size="small" :src="scope.row.avatar"></el-avatar>
          <span>{{ scope.row.nickname }}</span>
        </template>
      </el-table-column>
      <!-- 假设数据直接展示 就用prop展示  如果不是直接展示字符串的话就要用插槽 -->
      <!-- scope.row    代表这一行数据 scope.$index代表下标-->
      <el-table-column prop="mobile" label="电话" show-overflow-tooltip>
      </el-table-column>

      <el-table-column prop="status" label="状态" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.status ? "启用" : "禁用" }}
        </template>
      </el-table-column>

      <el-table-column prop="status" label="创建时间" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.created_at | filtime }}
        </template>
      </el-table-column>

      <el-table-column prop="status" label="操作" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            round
            @click="edit(scope.row, scope.$index)"
            >修改</el-button
          >
          <el-button
            type="success"
            size="mini"
            round
            @click="remove(scope.$index)"
            >删除</el-button
          >
          <el-button
            size="mini"
            round
            v-if="scope.row.status"
            @click="changeStatus(scope.$index)"
            >禁用</el-button
          >
          <el-button
            size="mini"
            round
            v-else
            @click="changeStatus(scope.$index)"
            >启用</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="prev, pager, next"
      :total="list.length"
      :page-size="pagesize"
      :current-page="pagenum"
      @current-change="changePage"
    >
    </el-pagination>
    <!-- :page-size="pagesize"  每页多少条
      :current-page="pagenum" 当前是第几页
      @current-change="changePage" 页码改变时候触发这个事件 -->

    <el-dialog title="编辑" :visible.sync="show">
      <el-form :model="form">
        <el-form-item label="用户名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="手机">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      copylist: [],
      form: {
        name: "",
        mobile: "",
      },

      show: false,
      editIndex: -1,
      ser: {
        name: "",
        mobile: "",
      },
      result: [],
      pagesize: 5,
      //每页多少条数据
      pagenum: 1,
      //当前的页码
    };
  },
  mounted() {
    this.getlist();
  },
  methods: {
    change(val) {
      console.log(val);
      this.result = val;
    },
    getlist() {
      this.$axios.get("list.json").then((res) => {
        this.list = res.data;
        this.copylist = res.data;
      });
    },
    remove(index) {
      this.list.splice(index, 1);
    },
    edit(item, index) {
      this.show = true;
      this.form.name = item.nickname;
      this.form.mobile = item.mobile;
      this.editIndex = index;
    },
    update() {
      this.show = false;
      this.list[this.editIndex].nickname = this.form.name;
      this.list[this.editIndex].mobile = this.form.mobile;
    },
    search() {
      this.list = this.copylist.filter((item) => {
        return (
          item.nickname.includes(this.ser.name) &&
          item.mobile.includes(this.ser.mobile)
        );
      });
    },
    reset() {
      this.list = this.copylist;
      this.ser.name = "";
      this.ser.mobile = "";
    },
    removeall() {
      this.list = this.list.filter((item) => {
        return this.result.includes(item) == false;
      });
    },
    changeStatus(index) {
      this.list[index].status = !this.list[index].status;
    },
    // start(index) {
    //   this.list[index].status = 1;
    // },

    changePage(val) {
      //这个事件会在 页码发生改变的时候触发
      // 默认的参数val代表 当前的页码
      this.pagenum = val;
    },
  },
  computed: {
    reslist() {
      return this.list.slice(
        (this.pagenum - 1) * this.pagesize,
        this.pagenum * this.pagesize
      );
    },
    //用计算属性进行分页的数据切割
  },
  components: {},
  filters: {
    filtime(val) {
      return new Date(val * 1000).toLocaleString();
    },
  },
};
</script>

<style lang="scss"></style>
